package com.ruoyi.controller.fanruan;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.jpjs.common.fanruan.JinpengMap;
import com.ruoyi.domain.jpjs.common.fanruan.JsProInfo;
import com.ruoyi.service.fanruan.IJinpengMapService;
import com.ruoyi.service.fanruan.IJsProInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目列表Controller
 *
 * @author ruoyi
 * @date 2020-11-12
 */
@Controller
@RequestMapping("/fanruan/program")
public class JsProInfoController extends BaseController
{
    private String prefix = "fanruan/program";

    @Autowired
    private IJsProInfoService jsProInfoService;

    @Autowired
    private IJinpengMapService jinpengMapService;

    @RequiresPermissions("fanruan:program:view")
    @GetMapping()
    public String program()
    {
        return "fanruan/program/program";
    }

    /**
     * 查询项目列表列表
     */
    @RequiresPermissions("fanruan:program:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JsProInfo jsProInfo)
    {
        startPage();
        List<JsProInfo> list = jsProInfoService.selectJsProInfoList(jsProInfo);
        return getDataTable(list);
    }

    /**
     * 导出项目列表列表
     */
    @RequiresPermissions("fanruan:program:export")
    @Log(title = "项目列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JsProInfo jsProInfo)
    {
        List<JsProInfo> list = jsProInfoService.selectJsProInfoList(jsProInfo);
        ExcelUtil<JsProInfo> util = new ExcelUtil<JsProInfo>(JsProInfo.class);
        return util.exportExcel(list, "program");
    }

    /**
     * 新增项目列表
     */
    @GetMapping("/add")
    public String add()
    {
        return "fanruan/program/add";
    }

    /**
     * 新增保存项目列表
     */
    @RequiresPermissions("fanruan:program:add")
    @Log(title = "项目列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JsProInfo jsProInfo)
    {
        // step1 根据区域查询城市表中是否存在该区域
        JinpengMap jinpengMap = new JinpengMap();
        jinpengMap.setCity(jsProInfo.getDistrictName());
        List<JinpengMap> jinpengMaps = jinpengMapService.selectJinpengMapList(jinpengMap);

        if(StringUtils.isNotEmpty(jinpengMaps)){
            // step2.1 存在,项目总数添加1
            jinpengMapService.updateProjectByCity(jinpengMap);
            return toAjax(jsProInfoService.insertJsProInfo(jsProInfo));
        }else{
            // step2.2 不存在,提示该项目中的区域不存在于轮播城市中
            return new AjaxResult(AjaxResult.Type.WARN, MessageUtils.message("program.add.notfind"));
        }
    }

    /**
     * 修改项目列表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JsProInfo jsProInfo = jsProInfoService.selectJsProInfoById(id);
        mmap.put("jsProInfo", jsProInfo);
        return "fanruan/program/edit";
    }

    /**
     * 修改保存项目列表
     */
    @RequiresPermissions("fanruan:program:edit")
    @Log(title = "项目列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JsProInfo jsProInfo)
    {
        return toAjax(jsProInfoService.updateJsProInfo(jsProInfo));
    }

    /**
     * 删除项目列表
     */
    @RequiresPermissions("fanruan:program:remove")
    @Log(title = "项目列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        // step1 根据id查询当前对应区域
        JsProInfo jsProInfo = new JsProInfo();
        jsProInfo.setId(Long.valueOf(ids));
        List<JsProInfo> jsProInfos = jsProInfoService.selectJsProInfoList(jsProInfo);

        // step2 根据区域将城市轮播表中区域对应项目数量减1
        JinpengMap jinpengMap = new JinpengMap();
        jinpengMap.setCity(jsProInfos.get(0).getDistrictName());
        jinpengMapService.updateProjectByCityForReduce(jinpengMap);

        // step3 删除项目信息,响应结果
        return toAjax(jsProInfoService.deleteJsProInfoByIds(ids));
    }
}
